package moneyx

import (
	"github.com/shopspring/decimal"
)

var TenDecimal decimal.Decimal = decimal.NewFromInt(10)
var oneHundredDecimal decimal.Decimal = decimal.NewFromInt(100)
var oneThousandDecimal decimal.Decimal = decimal.NewFromInt(1000)
var tenThousandDecimal decimal.Decimal = decimal.NewFromInt(10000)

// 分转元
func Fen2Yuan(fen int64) float64 {
	y, _ := decimal.NewFromInt(fen).Div(oneHundredDecimal).Truncate(2).Float64()
	return y
}

// 元转分
func Yuan2Fen(yuan float64) int64 {
	f, _ := decimal.NewFromFloat(yuan).Mul(oneHundredDecimal).Truncate(0).Float64()
	return int64(f)
}

// int => string
// s := strconv.Itoa(i)
// s := strconv.FormatInt(int64(i), 10)
//
// uint => string
// s := strconv.FormatUint(uint64(i), 10).
//
// string => int
// i, err := strconv.Atoi(s)
//
// string => int64
// i, err := strconv.ParseInt(s, 10, 64)
func ParseGem(amount string) (res int64, err error) { // #24.07.11# 单位分，扩大一千倍存储
	f, err := decimal.NewFromString(amount)
	if err != nil {
		return 0, err
	}

	return f.Mul(oneThousandDecimal).IntPart(), nil
}

func FormatGem(amount int64) (res string) { // #24.07.11#
	return decimal.NewFromInt(amount).Div(oneThousandDecimal).Truncate(2).String()
}

func ParseAmount(amount string) (res int64, err error) { // #24.07.11# 单位元，扩大一万倍存储
	f, err := decimal.NewFromString(amount)
	if err != nil {
		return 0, err
	}

	return f.Mul(tenThousandDecimal).IntPart(), nil
}

func FormatAmount(amount int64) (res string) { // #24.07.11#
	return decimal.NewFromInt(amount).Div(tenThousandDecimal).Truncate(2).String()
}

func FormatDiscountValue(discountValue string) (res string, err error) { // ##25.03.17## 打几折
	f, err := decimal.NewFromString(discountValue)
	if err != nil {
		return "", err
	}

	return decimal.NewFromInt(100).Sub(f).Div(TenDecimal).Truncate(1).String(), nil
}

func ParseWeight(weight string) (res int64, err error) { // #24.07.11# 单位克，扩大一百倍存储
	f, err := decimal.NewFromString(weight)
	if err != nil {
		return 0, err
	}

	return f.Mul(oneHundredDecimal).IntPart(), nil
}

func FormatWeight(weight int64) (res string) { // #24.07.11#
	return decimal.NewFromInt(weight).Div(oneHundredDecimal).Truncate(3).String()
}

func ParseVolume(volume string) (res int64, err error) {
	f, err := decimal.NewFromString(volume)
	if err != nil {
		return 0, err
	}

	return f.Mul(oneHundredDecimal).IntPart(), nil
}

func FormatVolume(volume int64) (res string) {
	return decimal.NewFromInt(volume).Div(oneHundredDecimal).Truncate(3).String()
}

func ParseDecimal(val string) (res decimal.Decimal, err error) {
	return decimal.NewFromString(val)
}

func FormatDecimal(val decimal.Decimal, fractional int32) (res string) {
	return val.Truncate(fractional).String()
}

func ParseAmountToFloat(val string) (res float64, err error) {
	f, err := decimal.NewFromString(val)
	if err != nil {
		return 0, err
	}

	res, _ = f.Truncate(2).Float64()

	return
}

func FormatDecimalToFen(val decimal.Decimal) (res int64) {
	return val.Mul(oneHundredDecimal).IntPart()
}

func ParseStar(star string, fractional int32) string {
	f, err := decimal.NewFromString(star)
	if err != nil {
		return ""
	}

	return f.Truncate(fractional).String()
}

func FormatStar(star float64, fractional int32) string {
	return decimal.NewFromFloat(star).Truncate(fractional).String()
}
